<template>
  <div>
    <el-dialog :title="formData.id ?'编辑经纪':'新增经纪'" :visible="showDialog" @close="closeFn">
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">

        <el-form-item label="经纪" prop="agent">
          <el-select v-model="formData.agent" placeholder="请选择">
            <el-option
              v-for="item in option"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="微信" prop="wechat">
          <el-input v-model="formData.wechat" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" type="textarea" resize="none" />
        </el-form-item>
        <el-form-item label="封面" prop="image">
          <Uploadimg ref="img1" />
          <el-input v-model="formData.image" placeholder="强制设置图片跳过上传" />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-row type="flex" justify="end">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button size="small" type="primary" @click="submit">确认</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { addClients, editClients, editClientsId } from '@/api/clients'
import { getAgents } from '@/api/agents'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        name: '',
        desc: '',
        image: '',
        wechat: '',
        email: '',
        phone: '',
        agent: {}

      },

      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }

        ],
        phone: [
          { required: true, message: '手机不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '格式不正确', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '手机不能为空', trigger: 'blur' },
          { pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: '格式不正确', trigger: 'blur' }
        ]

      },
      option: [],
      value: ''
    }
  },
  async created() {
    const res = await getAgents({
      _limit: -1
    })
    this.option = res.data
    // console.log(1111111, this.option)
  },
  methods: {
    // 获取指定分类
    async editClientsId(id) {
      const res = await editClientsId(id)
      console.log(56, this.option, res)
      this.formData = res.data
      this.$refs.img1.imageUrl = res.data.image
    },
    closeFn() {
      this.$emit('closeEvent')
      //   使用语法：
      // 子组件：this.$emit('update:要修改的props属性名',更新的值)
      // 父组件
      // <AddRole 属性名.sync = '父组件中的变量'></AddRole>
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        name: '',
        desc: '',
        image: '',
        wechat: '',
        email: '',
        phone: '',
        agent: ''

      }
      this.$refs.img1.imageUrl = ''
      // 清空效验
      this.$refs.form.resetFields()
    },
    submit() {
    //   await addClients(this.formData)

      this.$refs.form.validate(async bool => {
        if (bool) {
          // 表单验证
          if (this.formData.id) {
            await editClients(this.formData)
          } else {
            // 效验通过，调用接口
            if (this.$refs.img1.percent > 0 && this.$refs.img1.percent < 100) {
              return this.$message.warning('正在上传中，请稍后保存')
            }
            this.formData.image = this.$refs.img1.imageUrl || null
            await addClients(this.formData)

            // console.log(123)
          }

          this.closeFn()
          //   更新列表
          this.$emit('updateEvent')
        }
      })
    }
  }
}
</script>
<style>

</style>
